package com.hnhp.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hnhp.dao.CategoryDao;
import com.hnhp.info.PageInfo;
import com.hnhp.model.Category;
import com.hnhp.model.CategoryQuery;
import com.hnhp.threadlocal.LocalPageInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service  // 把当前类 交给springboot容器
public class CategoryService {

    // 引入dao接口
    @Autowired
    private CategoryDao dao;

    public List<Category> findAll (){
        return dao.findAll();
    }

    //根据id name 批量查询 id name是不是必填项
    public List<Category> select(CategoryQuery query){
        if (query.getPageNum()!= null && query.getPageSize()!=null){
            // 前台发送的
            PageHelper.startPage(query.getPageNum(),query.getPageSize());
        }

        List<Category> categoryList = dao.select(query);
        if (categoryList instanceof Page){
            Page page= (Page) categoryList;
            long total = page.getTotal();
            System.out.println(total);
            LocalPageInfoUtil.set(PageInfo.builder().Total(total).build());
        }
        return categoryList;
    }

   public Integer insert(Category category){
        return dao.insert(category);
    }
    public Integer update(Category category){
        return dao.update(category);
    }
    // 软删除
    public Integer updateStatus(Integer id){
        return dao.updateStatus(id,1);
    }
}
